<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['gender'])){
		$params['gender']=intval($_RQ['gender']);
		$return['params']['gender']=$_RQ['gender'];
	}
	if(!empty($_RQ['qunid'])){
		$params['qunid']=intval($_RQ['qunid']);
		$return['params']['qunid']=$_RQ['qunid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$params0=$params1=$params2=$params3=$params;
	$params0['buystatus']=0;
	$params1['buystatus']=1;
	$params2['buystatus']=2;
	$params3['buystatus']=3;
	$list0=pdo_getslice('jy_scrm_member',$params0,array($pindex,$psize),$total,$fields,'','id desc');
	if($list0){
		foreach($list0 as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
		}
	}
	$list1=pdo_getslice('jy_scrm_member',$params1,array($pindex,$psize),$total,$fields,'','id desc');
	if($list1){
		foreach($list1 as &$item){
		}
	}
	$list2=pdo_getslice('jy_scrm_member',$params2,array($pindex,$psize),$total,$fields,'','id desc');
	if($list2){
		foreach($list2 as &$item){
		}
	}
	$list3=pdo_getslice('jy_scrm_member',$params3,array($pindex,$psize),$total,$fields,'','id desc');
	if($list3){
		foreach($list3 as &$item){
		}
	}
	$return['list0num']=pdo_count('jy_scrm_member',$params0,'id');
	$return['list1num']=pdo_count('jy_scrm_member',$params1,'id');
	$return['list2num']=pdo_count('jy_scrm_member',$params2,'id');
	$return['list3num']=pdo_count('jy_scrm_member',$params2,'id');
	$return['list0']=$list0;
	$return['list1']=$list1;
	$return['list2']=$list2;
	$return['list3']=$list3;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'基本信息','type'=>'member'),
			array('name'=>'pcd','title'=>'区域','type'=>'string'),
			array('name'=>'groupid','title'=>'分组','type'=>'select','options'=>'groups'),
			array('name'=>'order_num','title'=>'标签','type'=>'string'),
			array('name'=>'comefrom','title'=>'来源','type'=>'radio','options'=>'user_froms'),
			array('name'=>'workerid','title'=>'归属成员','type'=>'select','options'=>'workers','option_value'=>'name'),
			array('name'=>'buystatus','title'=>'跟进状态','type'=>'radio','options'=>'user_buystatus'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'客户名称','type'=>'string','width'=>'15'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string','width'=>'15'),
			array('name'=>'gender','title'=>'客户性别','type'=>'radio','options'=>'sex','width'=>'15'),
			array('name'=>'isfans','title'=>'是否粉丝','type'=>'radio','options'=>'isfans','width'=>'15'),
			array('name'=>'link_time','title'=>'联系时间','type'=>'radio','options'=>'link_times','width'=>'15'),
			array('name'=>'user_paixu','title'=>'排序方式','type'=>'radio','options'=>'user_paixu','width'=>'15'),
			array('name'=>'comefrom','title'=>'客户来源','type'=>'radio','options'=>'user_froms','width'=>'15'),
			array('name'=>'qunid','title'=>'选择群聊','type'=>'select','options'=>'quns','width'=>'15'),
			array('type'=>'choose','width'=>15,'title'=>'部门成员','name'=>'workerid','action'=>'jy_merchscrm/merch.worker.get_worker/'),
			array('name'=>'createtime','width'=>15,'title'=>'添加时间','type'=>'time_range'),
		),	
	);
	$return['user_buystatus']=$this->user_buystatus;
	$return['user_paixu']=$this->user_paixu;
	$return['sex']=array('未知','男','女');
	$return['isfans']=array('否','是');
	$return['user_froms']=$this->user_froms;
	$return['link_times']=$this->link_times;
	$return['quns']=pdo_getall('jy_scrm_qun',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_scrm_member_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	//$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回会员列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'会员昵称','name'=>'nickname','help'=>''),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'select','title'=>'会员组','name'=>'groupid','options'=>'group','help'=>''),
					array('type'=>'select','title'=>'会员等级','name'=>'level','options'=>'levels','help'=>''),
					array('type'=>'string','title'=>'真实姓名','name'=>'realname','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'注册时间','name'=>'createtime','help'=>'','read'=>true),
					array('type'=>'switch','title'=>'加入黑名单','name'=>'isblack','help'=>'设置黑名单后，此会员无法访问商城'),
					
				)
			),
				array(
				'title'=>'交易信息',
				'list'=>array(
					array('type'=>'string','title'=>'成交订单数','name'=>'order_count','help'=>'','read'=>true),
					array('type'=>'string','title'=>'成交额','name'=>'order_money','help'=>'','read'=>true),
				)
			),
		)
	);
	
	$return['group']=pdo_getall('jy_scrm_member_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['levels']=pdo_getall('jy_scrm_member_level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'groupid'=>intval($postdata['groupid']),
		'level'=>intval($postdata['level']),
		'isblack'=>intval($postdata['isblack']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		pdo_insert('jy_scrm_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_member',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='toworker'){
	$post=get_postdata();
	$id=intval($post['id']);
	$member=pdo_get('jy_scrm_member',array('id'=>$id));
	if(!$member['workerid']){
		$member['workerid']=model('jy_scrm/worker')->get_workerid($member['follow_user_userid']?:$member['follow_user_oper_userid']);
	}
	$workerid=$member['workerid'];
	$content='《'.$member['name'].'》客户，长时间未跟进，请及时跟进！';
	$url=custom_url('jy_merchscrm/member.item//id='.$id,true);
	$res=model('jy_scrm/task')->onegenjin($workerid,$content,$url);
	if($res){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'workerid'=>$workerid,
			'userid'=>$id,
			'ismsg'=>1,
			'title'=>'后台跟进提醒',
			'content'=>$content,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_scrm_worker_linelog',$data);
		exi('提醒成功！');
	}else{
		exi('提醒失败！','error');
	}
}